package com.yin.myproject.zk.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 模拟的共享资源
 *
 * @author Eason
 * @create 2017-12-25 9:37
 **/
public class FakeLimitedResource {

    private final AtomicBoolean inUse = new AtomicBoolean(false);

    public void use() throws InterruptedException {
        if (!inUse.compareAndSet(false, true)) {
            throw new IllegalStateException("Needs to be used by one client at a time");
        }

        try {
            TimeUnit.SECONDS.sleep(3);
        } finally {
            inUse.set(false);
        }
    }
}
